/**
 * Created by Administrator on 2016/8/18.
 */
var user_tbody = $('#user_tbody');
var terminnals_tbody = $('#terminnal_tbody');
var  category;
$(function(){


    $.ajax({
        type:'GET',
        url:host+"/orginno",
        async:false,
        success:function (data) {
            category = data.list;
        },
        error: function(XMLHttpRequest){
            alert( "请求错误 " + XMLHttpRequest.responseText);
        }
    });

    $.ajax({
        type:'GET',
        url:host+"/push/terminnals",
        async:false,
        success:function (data) {
            if(data.code == 1){
                change_terminnal(data);
            }else{
                alert(data.msg);
            }

        },
        error: function(XMLHttpRequest){
            alert( "请求错误 " + XMLHttpRequest.responseText);
        }
    });

    $.ajax({
        type:'GET',
        url:host+"/push/users",
        async:false,
        success:function (data) {
            if(data.code == 1){
                change_user(data);
            }else{
                alert(data.msg);
            }

        },
        error: function(XMLHttpRequest){
            alert( "请求错误 " + XMLHttpRequest.responseText);
        }
    });

});

function change_user(data){
    user_tbody.empty();
    $.each(data.list,function(i,item){
        var tr = ' <tr>'+
            ' <td >'+item.userid+'</td>'+
            '<td>'+getOrginnoName(item.originno)+'</td>'+
            '<td>'+item.key+'</td>'+
            ' </tr>';
        user_tbody.append(tr);
    });
}

function change_terminnal(data){

    terminnals_tbody.empty();
    $.each(data.list,function(i,item){
        var tr = ' <tr>'+
            ' <td >'+item.terminalno+'</td>'+
            '<td>'+getOrginnoName(item.originno)+'</td>'+
            '<td>'+item.key+'</td>'+
            ' </tr>';
        terminnals_tbody.append(tr);
    });
}

function getOrginnoName(originno){
    var address;
    $.each(category, function (i, item) {
        if (item.orginno == originno) {

            address =  item.address;
        }
    });
    return address;
}